package org.apache.flink.runtime.metrics.groups;

import org.apache.flink.api.common.ConsumerID;
import org.apache.flink.runtime.metrics.NoOpMetricRegistry;
import org.apache.flink.runtime.resourceconsumer.ConsumeAttemptID;
import static org.apache.flink.runtime.resourceconsumer.ConsumeAttemptID.randomId;

/** A collection of safe drop-in replacements for existing {@link ComponentMetricGroup}s. */
public class UnregisteredMetricGroups {

    private UnregisteredMetricGroups() {}

    public static ProcessMetricGroup createUnregisteredProcessMetricGroup() {
        return new UnregisteredProcessMetricGroup();
    }

    public static ResourceManagerMetricGroup createUnregisteredResourceManagerMetricGroup() {
        return new UnregisteredResourceManagerMetricGroup();
    }

    public static SlotManagerMetricGroup createUnregisteredSlotManagerMetricGroup() {
        return new UnregisteredSlotManagerMetricGroup();
    }

    public static ResourceProviderMetricGroup createUnregisteredTaskManagerMetricGroup() {
        return new UnregisteredTaskManagerMetricGroup();
    }

    public static ResourceProviderConsumerMetricGroup createUnregisteredTaskManagerJobMetricGroup() {
        return new UnregisteredTaskManagerJobMetricGroup();
    }

    public static ResourceSlotMetricGroup createUnregisteredTaskMetricGroup() {
        return new UnregisteredTaskMetricGroup();
    }
    
    /** A safe drop-in replacement for {@link ProcessMetricGroup ProcessMetricGroups}. */
    public static class UnregisteredProcessMetricGroup extends ProcessMetricGroup {
        private static final String UNREGISTERED_HOST = "UnregisteredHost";

        public UnregisteredProcessMetricGroup() {
            super(NoOpMetricRegistry.INSTANCE, UNREGISTERED_HOST);
        }
    }

    /**
     * A safe drop-in replacement for {@link ResourceManagerMetricGroup
     * ResourceManagerMetricGroups}.
     */
    public static class UnregisteredResourceManagerMetricGroup extends ResourceManagerMetricGroup {
        private static final String UNREGISTERED_HOST = "UnregisteredHost";

        UnregisteredResourceManagerMetricGroup() {
            super(NoOpMetricRegistry.INSTANCE, UNREGISTERED_HOST);
        }
    }

    /** A safe drop-in replacement for {@link SlotManagerMetricGroup SlotManagerMetricGroups}. */
    public static class UnregisteredSlotManagerMetricGroup extends SlotManagerMetricGroup {
        private static final String UNREGISTERED_HOST = "UnregisteredHost";

        UnregisteredSlotManagerMetricGroup() {
            super(NoOpMetricRegistry.INSTANCE, UNREGISTERED_HOST);
        }
    }

    /** A safe drop-in replacement for {@link ResourceProviderMetricGroup}s. */
    public static class UnregisteredTaskManagerMetricGroup extends ResourceProviderMetricGroup {
        private static final String DEFAULT_HOST_NAME = "UnregisteredHost";
        private static final String DEFAULT_TASKMANAGER_ID = "0";

        protected UnregisteredTaskManagerMetricGroup() {
            super(NoOpMetricRegistry.INSTANCE, DEFAULT_HOST_NAME, DEFAULT_TASKMANAGER_ID);
        }
    }

    /** A safe drop-in replacement for {@link ResourceProviderConsumerMetricGroup}s. */
    public static class UnregisteredTaskManagerJobMetricGroup extends ResourceProviderConsumerMetricGroup {
        private static final ConsumerID DEFAULT_JOB_ID = new ConsumerID(0, 0);
        private static final String DEFAULT_JOB_NAME = "UnregisteredJob";

        public UnregisteredTaskManagerJobMetricGroup() {
            super(
                    NoOpMetricRegistry.INSTANCE,
                    new UnregisteredTaskManagerMetricGroup(),
                    DEFAULT_JOB_ID,
                    DEFAULT_JOB_NAME);
        }

        @Override
        public ResourceSlotMetricGroup addTask(
                final ConsumeAttemptID executionAttemptID, final String taskName) {
            return createUnregisteredTaskMetricGroup();
        }
    }

    /** A safe drop-in replacement for {@link ResourceSlotMetricGroup}s. */
    public static class UnregisteredTaskMetricGroup extends ResourceSlotMetricGroup {
        private static final ConsumeAttemptID DEFAULT_ATTEMPT_ID = randomId();
        private static final String DEFAULT_TASK_NAME = "UnregisteredTask";

        protected UnregisteredTaskMetricGroup() {
            super(
                    NoOpMetricRegistry.INSTANCE,
                    new UnregisteredTaskManagerJobMetricGroup(),
                    DEFAULT_ATTEMPT_ID,
                    DEFAULT_TASK_NAME);
        }

    }

}
